﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using NHibernate.Cfg;
using System.Collections;
using System.Reflection;
using TvTipper.Core;
using TvTipper.Entities;
using TvTipper.Controls;
using TvTipper.Api;

namespace TvTipper
{
    public partial class MainForm : Form
    {
        private string m_lastPersonNameSearch;
        private TvProgramSearchQuery m_lastQuery;

        public MainForm()
        {
            InitializeComponent();
            m_searchType.SelectedIndex = 0;

            // load the year list
            for (int i = 2013; i >= 1930; i--)
            {
                m_year.Items.Add(i);
            }


            //init info panels
            m_entityInfo.SetInfoPanel(typeof(Person), new PersonInfoPanel());
            m_entityInfo.SetInfoPanel(typeof(TvProgram), new TvProgramInfoPanel());

            // load factors
            var factors = Services.EntityManager.RankFactors;
            m_factorActor.Value = (int)(factors["actor"] * 10);
            m_factorCreator.Value = (int)(factors["creator"] * 10);
            m_factorGenre.Value = (int)(factors["genre"] * 10);
        }

        private void m_searchType_SelectedValueChanged(object sender, EventArgs e)
        {
            m_searchTabControl.SelectedIndex = m_searchType.SelectedIndex;
        }

        private void OnSearchPersonClicked(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(m_namePerson.Text))
            {
                m_lastPersonNameSearch = m_namePerson.Text;
                DoSearchPerson();
            }
        }

        private void DoSearchPerson()
        {
            var md = Services.DataAccessPortal.SearchPersonsByName(m_lastPersonNameSearch);
            m_personBS.DataSource = new BindingList<PersonMetadata>(md);

            if (md.Count == 0)
            {
                MessageBox.Show("No results were found", "TvTipper", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void OnCurrentPersonChanged(object sender, EventArgs e)
        {
            var md = (PersonMetadata) m_personBS.Current;
            var person = Services.DataAccessPortal.GetPersonById(md.Id);
            if (person == null)
            {
                //ErrorHandler.HandlerError();
            }

            m_entityInfo.SetEntity(person);
        }

        private void OnSearchProgramClicked(object sender, EventArgs e)
        {
            int year = 0;
            if (!string.IsNullOrEmpty(m_year.Text) && !Int32.TryParse(m_year.Text, out year))
            {
                return;
            }

            var q = new TvProgramSearchQuery() 
            { 
                Name = m_textNameSer.Text,
                Year = m_year.Text 
            };

            if (!q.IsEmpty)
            {
                m_lastQuery = q;
                DoSearchProgram();
            }
        }

        private void DoSearchProgram()
        {
            var md = Services.DataAccessPortal.SearchTvProgram(m_lastQuery);
            m_programsBS.DataSource = new BindingList<TvProgramMetadata>(md);

            if (md.Count == 0)
            {
                MessageBox.Show("No results were found", "TvTipper", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void OnCurrentProgramChanged(object sender, EventArgs e)
        {
            var md = (TvProgramMetadata)((BindingSource)sender).Current;
            if (md != null)
            {
                var program = Services.DataAccessPortal.GetTvProgramById(md.Id);
                if (program == null)
                {
                    //ErrorHandler.HandlerError();
                }

                m_entityInfo.SetEntity(program);
            }
        }

        private void OnEnterRecomandations(object sender, EventArgs e)
        {
            if (!Services.EntityManager.RecommandationUpdated)
            {
                DoGetRecommandations();
            }
        }

        private void OnMainToolbarItemClicked(object sender, EventArgs e)
        {
            var btn = (RadioButton) sender;
            m_rootTab.SelectedIndex = int.Parse(btn.Tag.ToString());
        }

        private void OnExitItemClicked(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void OnImportItemClicked(object sender, EventArgs e)
        {
            var dlg = new DataImportForm();
            dlg.ShowDialog();
        }

        private void OnRefreshClicked(object sender, EventArgs e)
        {
            if (m_rootTab.SelectedIndex == 0)
            {
                DoGetRecommandations();
            }
            else if (m_rootTab.SelectedIndex == 1)
            {
                if (m_searchTabControl.SelectedIndex == 0)
                {
                    DoSearchPerson();
                }
                else
                {
                    DoSearchProgram();
                }
            }
        }

        private void DoGetRecommandations()
        {
            var programs = Services.DataAccessPortal.GetTopTvPrograms(0, 30);
            m_recommandedBS.DataSource = new BindingList<TvProgramMetadata>(programs);
        }

        private void OnSaveFactorsClicked(object sender, EventArgs e)
        {
            var factors = new Dictionary<string, float>();
            factors["actor"] = (float)m_factorActor.Value / 10;
            factors["creator"] = (float)m_factorCreator.Value / 10;
            factors["genre"] = (float)m_factorGenre.Value / 10;

            Services.EntityManager.RankFactors = factors;
            MessageBox.Show("Rank factors were updated", "TvTipper", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void OnAboutClicked(object sender, EventArgs e)
        {
            new AboutForm().ShowDialog();
        }
    }
}
